import random
import time
from ctypes import *
import pykeyboard
from pykeyboard import *
import keyboard  # 监听键盘
from configobj import ConfigObj

conf = {}

dd_dll = windll.LoadLibrary('./DD94687.64.dll')

time.sleep(2)
st = dd_dll.DD_btn(0) #DD Initialize
if st==1:
    print("OK")
else:
    print("Error")
    exit(101)

class PyAuto:

    def read_config(self):
        global conf
        conf = ConfigObj(r'config.ini', encoding='utf-8')
        print(conf)

        value = conf['key']['q']

        print(value)

    def button_key(self, x):
        global dd_dll
        # keyboard.add_hotkey('page up', lambda: keyboard.write('foobar'))
        for k in conf['key'][x]:
            dd_dll.DD_key(709, 1)
            # keyboard.press(k)
            time.sleep(0.5)
            dd_dll.DD_key(709, 2)
            # keyboard.release(k)
            # print(k)

    def run(self):
        self.read_config()
        print(keyboard.get_hotkey_name())
        keyboard.add_hotkey("q", self.button_key, args=("q",))
        keyboard.add_hotkey("w", self.button_key, args=("w",))
        keyboard.add_hotkey("e", self.button_key, args=("e",))
        keyboard.add_hotkey("r", self.button_key, args=("r",))
        keyboard.add_hotkey("t", self.button_key, args=("t",))
        keyboard.add_hotkey("y", self.button_key, args=("y",))
        # 按ctrl+alt输出b
        keyboard.wait("q")

    def close(self):
        keyboard.clear_all_hotkeys()
        # keyboard.wait("ctrl+z")


if __name__ == '__main__':
    p = PyAuto()
    p.run()
